package org.example.myleet.p1185;

public class Solution {

    private static final int[] DAYS_OF_MONTH = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    private static final String[] WEEKDAY = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    public String dayOfTheWeek(int day, int month, int year) {
        //1971-01-01 = Friday
        int startYear = 1971;
        int startMonth = 1;
        int startDay = 1;
        int startWeekday = 5;
        int d = 0;
        //计算过去的年份所含天数
        int diffYear = year - startYear;
        for (int i = 0; i < diffYear; ++i) {
            int y = startYear + i;
            //闰年+1天
            if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
                ++d;
            }
            d += 365;
        }
        //不满一年的部分计算过去的月数的天数
        int diffMonth = month - startMonth;
        for (int i = 0; i < diffMonth; ++i) {
            int m = startMonth + i;
            d += DAYS_OF_MONTH[m];
        }
        if (month > 2) {
            //2月份以上时，闰年+1天
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
                ++d;
            }
        }
        //不满一月的部分计算过去的天数
        int diffDay = day - startDay;
        for (int i = 0; i < diffDay; ++i) {
            ++d;
        }
        //对7取余计算星期几
        int wd = ((d % 7) + startWeekday) % 7;
        return WEEKDAY[wd];
    }
}
